﻿using System;
using System.IO;
using System.Text;

namespace Common.IO
{
    public static class FileEx
    {
        public static void WriteAlternateText(String path, String streamName, String text)
        {
            FileEx.WriteAlternateText(path, streamName, text, Encoding.UTF8);
        }

        public static void WriteAlternateText(String path, String streamName, String text, Encoding encoding)
        {
            using (AlternateFileStream stream = new AlternateFileStream(path, streamName, FileMode.Create, FileAccess.Write))
            {
                using (StreamWriter writer = new StreamWriter(stream, encoding))
                {
                    writer.Write(text);
                    writer.Flush();
                }
            }
        }

        public static String ReadAlternateText(String path, String streamName)
        {
            return FileEx.ReadAlternateText(path, streamName, Encoding.UTF8);
        }

        public static String ReadAlternateText(String path, String streamName, Encoding encoding)
        {
            using (AlternateFileStream stream = new AlternateFileStream(path, streamName, FileMode.Open, FileAccess.Read))
            {
                using (StreamReader reader = new StreamReader(stream, encoding))
                {
                    return reader.ReadToEnd();
                }
            }
        }
    }
}
